package com.example.administrator.baikangxing.activity;

import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;
import com.example.administrator.baikangxing.bean.StepBean;
import com.example.administrator.baikangxing.config.Constants;
import com.example.administrator.baikangxing.config.Url;
import com.example.administrator.baikangxing.utils.CommomUtil;
import com.example.administrator.baikangxing.utils.HttpUtil;
import com.example.administrator.baikangxing.utils.LogUtil;
import com.example.administrator.baikangxing.utils.TimeUtils;
import com.example.administrator.baikangxing.utils.ToastUtil;
import com.example.administrator.baikangxing.utils.Utils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;

public class StatisticsStepActivity extends BaseActivity implements View.OnClickListener {

    private TextView statics_steps_tv_carle;
    private TextView statics_step_tv_month;
    private TextView statics_step_tv_day;
    private TextView statics_step_tv_week;
    private LinearLayout statics_step_ll_day;
    private LinearLayout statics_step_ll_week;
    private LinearLayout statics_step_ll_month;
    private String select_num;
    private String[] values;
    ColumnChartData columnData;
    private int[] score;
    private String[] date;
    private ColumnChartView statics_step_chart;
    private TextView statics_steps_tv_mileage;
    private TextView statics_steps_tv_number;

    @Override
    public int getContentViewLayoutId() {
        return R.layout.activity_statics_step;
    }

    @Override
    public void initView() {
        base_ll_title.setBackgroundResource(R.color.steps_bg);
        setTitles("步数统计");
        setRightIcon(R.drawable.right_share);
        statics_step_tv_day = findView(R.id.statics_step_tv_day);
        statics_step_tv_week = findView(R.id.statics_step_tv_week);
        statics_step_tv_month = findView(R.id.statics_step_tv_month);
        statics_step_ll_day = findView(R.id.statics_step_ll_day);
        statics_step_ll_week = findView(R.id.statics_step_ll_week);
        statics_step_ll_month = findView(R.id.statics_step_ll_month);
        statics_step_chart = findView(R.id.statics_step_chart);
        statics_steps_tv_carle = findView(R.id.statics_steps_tv_carle);
        statics_steps_tv_mileage = findView(R.id.statics_steps_tv_mileage);
        statics_steps_tv_number = findView(R.id.statics_steps_tv_number);
        statics_step_ll_day.setOnClickListener(this);
        statics_step_ll_week.setOnClickListener(this);
        statics_step_ll_month.setOnClickListener(this);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        select_num = OperateData.getStringData(this, Constants.SELECT_DEVICE_NUM);
        getDatafromNet(0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.statics_step_ll_day:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    statics_step_tv_day.setBackground(getResources().getDrawable(R.drawable.blue_circle));
                }
                statics_step_tv_day.setTextColor(getResources().getColor(R.color.white));
                statics_step_tv_week.setTextColor(getResources().getColor(R.color.gray));
                statics_step_tv_month.setTextColor(getResources().getColor(R.color.gray));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    statics_step_tv_week.setBackground(getResources().getDrawable(R.drawable.gray_circle));
                    statics_step_tv_month.setBackground(getResources().getDrawable(R.drawable.gray_circle));
                }
                getDatafromNet(0);
                break;
            case R.id.statics_step_ll_week:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    statics_step_tv_day.setBackground(getResources().getDrawable(R.drawable.gray_circle));
                }
                statics_step_tv_day.setTextColor(getResources().getColor(R.color.gray));
                statics_step_tv_week.setTextColor(getResources().getColor(R.color.white));
                statics_step_tv_month.setTextColor(getResources().getColor(R.color.gray));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    statics_step_tv_week.setBackground(getResources().getDrawable(R.drawable.blue_circle));
                    statics_step_tv_month.setBackground(getResources().getDrawable(R.drawable.gray_circle));
                }
                getDatafromNet(1);
                break;
            case R.id.statics_step_ll_month:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    statics_step_tv_day.setBackground(getResources().getDrawable(R.drawable.gray_circle));
                }
                statics_step_tv_day.setTextColor(getResources().getColor(R.color.gray));
                statics_step_tv_week.setTextColor(getResources().getColor(R.color.gray));
                statics_step_tv_month.setTextColor(getResources().getColor(R.color.white));
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    statics_step_tv_week.setBackground(getResources().getDrawable(R.drawable.gray_circle));
                    statics_step_tv_month.setBackground(getResources().getDrawable(R.drawable.blue_circle));
                }
                getDatafromNet(2);
                break;
            default:
                break;
        }
    }

    /*从网络请求数据*/
    private void getDatafromNet(final int type) {
        switch (type) {
            case 0:
                values = new String[]{select_num, "day", TimeUtils.getAgoDay(),TimeUtils.getNowDay()};
                LogUtil.e("开始的日期为："+ TimeUtils.getAgoDay());
                LogUtil.e("今天的日期为："+TimeUtils.getNowDay());
                break;
            case 1:
                values = new String[]{select_num, "week",String.valueOf(Integer.valueOf(TimeUtils.getWeek())-3), TimeUtils.getWeek()};
                LogUtil.e("开始的日期为："+ String.valueOf(Integer.valueOf(TimeUtils.getWeek())-3));
                LogUtil.e("今天的日期为："+TimeUtils.getWeek());
                break;
            case 2:
                values = new String[]{select_num, "month", String.valueOf(Integer.valueOf(TimeUtils.getNowDay().substring(5, 7)) - 3), TimeUtils.getNowDay().substring(5, 7)};
                LogUtil.e("开始的日期为："+ String.valueOf(Integer.valueOf(TimeUtils.getNowDay().substring(5, 7)) - 3));
                LogUtil.e("今天的日期为："+TimeUtils.getNowDay().substring(5, 7));

                break;
            default:
                break;
        }
        String url = Url.base_url + Constants.HEALTH_TARGET[4];
        HttpUtil.getInstance().postString(url, new String[]{"devid", "ext", "begin","end"}, values, new HttpUtil.CallBackPostString() {
            @Override
            public void onSuccess(JSONObject successString) {
                try {
                    String errorCode = successString.getString("errorCode");
                    if (errorCode.equals("0")) {
                        LogUtil.e(successString.toString());
                        JSONArray message = successString.getJSONArray("message");
                        ArrayList<StepBean> stepList = CommomUtil.parserJsonArrayToList(message, StepBean.class);
                        switch (type){
                            case 0:
                                date = new String[stepList.size()];
                                score = new int[stepList.size()];
                                for (int i = 0; i < stepList.size(); i++) {
                                    date[i] = stepList.get(i).getTime().substring(5,10);
                                    LogUtil.e("对应的日期为"+date[i]);
                                    score[i] = Utils.getAbs(stepList.get(i).getPassometer());
                                }
                                setLine();
                                break;
                            case 1:
                                date = new String[stepList.size()];
                                score = new int[stepList.size()];
                                for (int i = 0; i < stepList.size(); i++) {
                                    date[i] = TimeUtils.getDateFromWeekNo(Integer.valueOf(TimeUtils.getNowTime().substring(0,4)), Integer.parseInt(stepList.get(i).getTime()));
                                    LogUtil.e("对应的日期为"+date[i]);
                                    score[i] = Utils.getAbs(stepList.get(i).getPassometer());
                                }
                                setLine();
                                break;
                            case 2:
                                date = new String[stepList.size()];
                                score = new int[stepList.size()];
                                for (int i = 0; i < stepList.size(); i++) {
                                    date[i] = stepList.get(i).getTime();
                                    LogUtil.e("对应的日期为"+date[i]);
                                    score[i] = Utils.getAbs(stepList.get(i).getPassometer());
                                }
                                setLine();
                                break;
                            default:
                                break;
                        }
                    } else if (errorCode.equals("105")) {
                        ToastUtil.showToast("暂无数据");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(JSONObject failString) {

            }

            @Override
            public void onProgress(long bytesWritten, long totalSize) {

            }
        });
    }

    private void setLine() {

        int numSubcolumns = 1;
        int numColumns = date.length;

        List<AxisValue> axisValues = new ArrayList<AxisValue>();
        List<Column> columns = new ArrayList<Column>();
        List<SubcolumnValue> values;
        for (int i = 0; i < numColumns; i++) {
            values = new ArrayList<SubcolumnValue>();
            for (int j = 0; j < numSubcolumns; j++) {
                values.add(new SubcolumnValue(score[i],
                        Color.WHITE));
            }
            // 点击柱状图就展示数据量
            axisValues.add(new AxisValue(i).setLabel(date[i]));

            columns.add(new Column(values).setHasLabelsOnlyForSelected(true));
        }

        columnData = new ColumnChartData(columns);

        columnData.setAxisXBottom(new Axis(axisValues).setHasLines(false)
                .setTextColor(Color.BLACK));
        columnData.setAxisYLeft(new Axis().setHasLines(false)
                .setTextColor(R.color.steps_bg).setLineColor(R.color.steps_bg).setMaxLabelChars(0));

        statics_step_chart.setColumnChartData(columnData);

        // Set value touch listener that will trigger changes for chartTop.

        // Set selection mode to keep selected month column highlighted.
        statics_step_chart.setValueSelectionEnabled(true);

        statics_step_chart.setZoomType(ZoomType.HORIZONTAL);

        statics_step_chart.setOnValueTouchListener(new ColumnChartOnValueSelectListener() {
            @Override
            public void onValueSelected(int i, int i1, SubcolumnValue subcolumnValue) {
                statics_steps_tv_number.setText((int) subcolumnValue.getValue()+"");
                statics_steps_tv_mileage.setText(Utils.calc(0, (int) subcolumnValue.getValue())+ getResources().getString(R.string.text_distance_mark));
                statics_steps_tv_carle.setText(Utils.calc(1,(int) subcolumnValue.getValue())+ getResources().getString(R.string.text_carle_mark));
            }

            @Override
            public void onValueDeselected() {

            }
        });

    }
}
